package com.ewuyong.concurrence.example.publish.singleton;

import com.ewuyong.concurrence.annotations.Recommend;
import com.ewuyong.concurrence.annotations.ThreadSafe;

/**
 * 枚举实现单例模式
 */
@ThreadSafe
@Recommend
public class EnumSingleton {

    private EnumSingleton() {

    }
    public static EnumSingleton getInstance() {
        return Singleton.INSTANCE.getInstance();
    }
    private enum Singleton {
        INSTANCE;

        private EnumSingleton singleton;

        //jvm 保证这个方法绝对只调用一次
        Singleton (){
            singleton = new EnumSingleton();
        }
        public EnumSingleton getInstance() {
            return singleton;
        }

    }
}
